package com.onesports.intelligent.k12.polarlight.controller.organization.config;

import cn.oneframework.common.utils.ResultUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onesports.framework.kit.common.valid.Json;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.ResetAccountPwdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.UpdateStatesDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.*;
import com.onesports.intelligent.k12.polarlight.domain.vo.organization.PageOrganizationCoachVO;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.coach.CoachService;
import com.onesports.intelligent.k12.polarlight.service.sys.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Array;
import java.util.List;

/**
 * @author wangcx
 * @since 2022-7-12
 */
@Api(value = "机构端", tags = "机构端-教练管理")
@RestController
@RequestMapping(ApiPathConstants.ORGANIZATION_COACH)
@RequiredArgsConstructor
@PreAuthorize("isAuthenticated()")
public class OrganizationCoachController {

    private final CoachService service;
    private final AccountService accountService;

    @PostMapping("/add")
    @ApiOperation(value = "新增教练")
    public ResponseEntity<String> add(@Valid @RequestBody InsertCoachDTO dto) {
        dto.setOrganizationId(SecurityUtils.getOrganizationId());
        service.add(dto);

        return ResultUtil.success("添加成功");
    }


    @PostMapping("/upd")
    @ApiOperation(value = "修改教练")
    public ResponseEntity upd(@Valid @RequestBody UpdateCoachDTO dto) {
        dto.setOrganizationId(SecurityUtils.getOrganizationId());
        service.upd(dto);
        return ResultUtil.success("修改成功");
    }


    @GetMapping("/page")
    @ApiOperation(value = "机构端分页查询教练")
    public ResponseEntity<IPage<PageOrganizationCoachVO>> page(@Valid PageOrganizationCoachDTO dto) {
        dto.setOrganizationId(SecurityUtils.getOrganizationId());
        return ResultUtil.success(this.service.organizationPage(new Page<>(dto.getPage(), dto.getRow()), dto));
    }

    @PostMapping("/is-sort")
    @ApiOperation(value = "机构端查看当前教练校区关联排序是否已存在")
    public ResponseEntity<List<String>> getSort(@RequestBody GetSortCoachDTO dto) {
        return ResultUtil.success(this.service.getSort(dto));
    }


    @PostMapping("/upd-status")
    @ApiOperation(value = "修改教练状态")
    public ResponseEntity updStatus(@Valid @RequestBody UpdateStatesDTO dto) {
        service.updStatus(dto);
        return ResultUtil.success();
    }


    @PostMapping("/reset-pwd")
    @ApiOperation(value = "重置密码")
    public ResponseEntity resetPwd(@Valid @RequestBody ResetAccountPwdDTO dto) {
        accountService.resetPwdByPhone(dto.getPhone(), null);
        return ResultUtil.success("密码重置成功");
    }



}